package com.example.demo.dao;

import com.example.demo.model.Articleinfo;
import com.example.demo.model.Userinfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    //通过uid来在articleinfo表中查询对应的文章信息
    @Select("select*from articleinfo where uid=#{uid} order by id desc")//按照id降序查询
    List<Articleinfo> getListByuid(@Param("uid") int uid);

    //通过文章id来删除文章
    @Delete("delete from articleinfo where id=#{aid} and uid=#{uid}")
    int del(@Param("aid")Integer aid,int uid); //aid是文章的id,uid是对应用户的id

    //添加文章
    @Insert("insert into articleinfo(title,content,uid) values(#{title},#{content},#{uid})")
    int add(Articleinfo articleinfo);//直接使用文章对象来传递,方便

    //通过文章id和用户uid来查询文章(用户uid也算是验证了一遍当前文章的归属人)
    @Select("select*from Articleinfo where id=#{aid} and uid=#{uid}")
    Articleinfo getArticleByIdAndUid(@Param("aid") int aid,@Param("uid")int uid);

    //修改文章
    @Update("update articleinfo set title=#{title},content=#{content} where id=#{id} and uid=#{uid}")
    int update(Articleinfo articleinfo);

    //根据id查询文章
    @Select("select*from articleinfo where id=#{aid}")
    Articleinfo getDatailById(@Param("aid")Integer aid);

    //根据uid查询用户发布的文章数量,使用聚合查询
    @Select("select count(*) from articleinfo where uid=#{uid}")
    public int getArtCountById(@Param("uid") Integer uid);

    //通过id修改文章阅读量属性
    @Update("update articleinfo set rcount=rcount+1 where id=#{aid}")
    public int incrementRCount(@Param("aid") int aid);

    //通过psize和分页公式算出的offset来实现分页查询
    @Select("select*from articleinfo order by id desc limit #{psize} offset #{offset}")
    List<Articleinfo> getListByPage(@Param("psize") int psize,@Param("offset") int offset);

    //统计文章数量
    @Select("select count(*) from articleinfo")
    int getCount();

}
